package com.youzhi.backend.exception;

import com.youzhi.backend.dto.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * 全局异常处理
 *
 * @author mengxu
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {




    @ExceptionHandler(YouZhiException.class)
    @ResponseBody
    public Result exceptionHandler(YouZhiException e) {
        log.error("controllerAdvice业务异常:",e);
        return Result.error(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(UnauthenticatedException.class)
    @ResponseBody
    public Result exceptionHandler(UnauthenticatedException e) {
        log.error("controllerAdvice未登陆异常:",e);
        return Result.error(EcpEnum.no_login);
    }

    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public Result exceptionHandler(AuthorizationException e) {
        log.error("controllerAdvice权限异常:", e);
        return Result.error(EcpEnum.no_authority);
    }


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        log.error("controllerAdvice全局异常:", e);
        return Result.error(EcpEnum.unknown_error);
    }
}
